//
//  LoginPwdChangeViewModel.m
//  CBT
//
//  Created by Mr.Zhang on 2018/11/29.
//  Copyright © 2018 Mr.Zhang. All rights reserved.
//

#import "LoginPwdChangeViewModel.h"
#import "PasswordManager.h"

@implementation LoginPwdChangeViewModel

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setUp];
    }
    return self;
}

- (void)setUp{
    
    _subMitCommand =  [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
        
        if ([NSString isBlankString:input[@"ID"]]) {
            [HFStatusView showFailureMessage:kLocalizedString(@"请输入手机",nil)];
            return [RACSignal error:nil];
        }else if ([NSString deptNumInputShouldNumber:input[@"ID"]] && ![NSString isInputRightPhoneNum:input[@"ID"]]) {
            [HFStatusView showFailureMessage:kLocalizedString(@"请输入正确手机号",nil)];
            return [RACSignal error:nil];
        }else if (![NSString deptNumInputShouldNumber:input[@"ID"]] && ![NSString validateEmail:input[@"ID"]]) {
            [HFStatusView showFailureMessage:kLocalizedString(@"请输入正确邮箱",nil)];
            return [RACSignal error:nil];
        }else if ([NSString isBlankString:input[@"code"]]) {
            [HFStatusView showFailureMessage:kLocalizedString(@"请输入验证码",nil)];
            return [RACSignal error:nil];
        }else if ([NSString isBlankString:input[@"password"]]){
            [HFStatusView showFailureMessage:kLocalizedString(@"请输入密码",nil)];
            return [RACSignal error:nil];
        }else if ([NSString stringWithFormat:@"%@",input[@"password"]].length < 8 || [NSString stringWithFormat:@"%@",input[@"password"]].length > 20){
            [HFStatusView showFailureMessage:kLocalizedString(@"密码长度错误",nil)];
            return [RACSignal error:nil];
        }
        
        return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
            
            [PasswordManager loginPwdChangeWithID:input[@"ID"] code:input[@"code"] pwd:input[@"password"] areaStr:input[@"area"] block:^(id data, HttpRequestResult result, NSString *msg) {
                if (result == kHttpRequestResultNormal) {
                    [HFStatusView showSuccessMessage:kLocalizedString(@"修改成功",nil)];
                    [[NSUserDefaults standardUserDefaults]setObject:input[@"password"] forKey:HEFANTV_PassWord];
                    [[NSUserDefaults standardUserDefaults] synchronize];
                    [subscriber sendNext:data];
                    [subscriber sendCompleted];
                }else{
                    [HFStatusView showFailureMessage:msg];
                    [subscriber sendError:nil];
                    [subscriber sendCompleted];
                }
            }];
            
            return [RACDisposable disposableWithBlock:^{
                NSLog(@"修改登录密码操作结束了");
            }];
        }];
    }];
    
    
    _sendCodeCommand =  [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
        if ([NSString isBlankString:input[@"ID"]]) {
            [HFStatusView showFailureMessage:kLocalizedString(@"请输入手机",nil)];
            return [RACSignal error:nil];
        }else if ([NSString deptNumInputShouldNumber:input[@"ID"]] && ![NSString isInputRightPhoneNum:input[@"ID"]]) {
            [HFStatusView showFailureMessage:kLocalizedString(@"请输入正确手机号",nil)];
            return [RACSignal error:nil];
        }else if (![NSString deptNumInputShouldNumber:input[@"ID"]] && ![NSString validateEmail:input[@"ID"]]) {
            [HFStatusView showFailureMessage:kLocalizedString(@"请输入正确邮箱",nil)];
            return [RACSignal error:nil];
        }
        
        return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
            
            [PasswordManager sendPhoneAndEmailCodeWithID:input[@"ID"] isPhone:[NSString isInputRightPhoneNum:input[@"ID"]] type:@"2" areaStr:input[@"code"] block:^(id data, HttpRequestResult result, NSString *msg) {
                if (result == kHttpRequestResultNormal) {
                    [HFStatusView showSuccessMessage:kLocalizedString(@"验证码发送成功",nil)];
                    [subscriber sendNext:data];
                    [subscriber sendCompleted];
                }else{
                    [HFStatusView showFailureMessage:msg];
                    [subscriber sendError:nil];
                    [subscriber sendCompleted];
                }
            }];
            
            return [RACDisposable disposableWithBlock:^{
                NSLog(@"%@发送验证码操作结束了",[[input allKeys] containsObject:@"email"] ? @"邮箱" : @"手机号");
            }];
        }];
    }];
    
}

@end
